<script setup>
import { defineEmits, ref,onMounted } from 'vue'
import {saveApi, updateApi} from "../../../api/grade.js";
import {ElMessage} from "element-plus";
//定义事件--父级
const emit = defineEmits(['flush']);

// 表单数据
const formData = ref({
  id: 0,
  askEtc: 0,
  askExam: 0,
  askStudy: 0,
  credit: 10,
  endDate: '',
  info: '',
  name: '',
  startDate: '',
  studyTime: 0,
  type: ''
})
// 表单验证
const formRules = ref({
  name: [
    { required: true, message: '请输入名称', trigger: 'blur' },
    { min: 2, max: 20, message: '请输入2~20个字符', trigger: 'blur' }
  ]
})

// 提交验证
const formRef = ref()
const onSubmit = () => {
  formRef.value.validate(async (isSuccess) => {
    if (isSuccess) {
      let r;
      const isadd=formData.value.id==0||formData.value.id==undefined;
      if(isadd) {
        r=await saveApi(formData.value);
      }else {
        r=await updateApi(formData.value);
      }
      if(r.code==0){
        ElMessage({
          type: 'success',
          message: isadd?'新增班级成功':'修改班级成功'})
        state.value=false
        emit('flush')
      }else {
        ElMessage.error(r.data)
      }
    }
  })
}
// 对话框状态
const state = ref(false)

// 暴露
defineExpose({
  state,
  formData
})
</script>
<template>
  <el-dialog
    v-model="state"
    :title="`${formData.id>0? '修改' : '添加'} 班级`"
    width="600"
    :before-close="() => (state = false)"
  >
    <!-- 内容 -->
    <el-form
      v-if="state"
      size="large"
      label-width="80"
      :model="formData"
      :rules="formRules"
      ref="formRef"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="班级名称" prop="title">
            <el-input placeholder="请输入班级名称" v-model="formData.name" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="班级类型">
            <el-select placeholder="请选择班级类型" v-model="formData.type">
              <el-option label="线上班" value="线上班"/>
              <el-option label="线下班" value="线下班"/>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="班级积分">
            <el-input type="number" placeholder="请输入班级积分" v-model="formData.credit" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="学习要求">
            <el-input placeholder="请输入学习要求" v-model="formData.askStudy" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="考试要求">
            <el-input placeholder="请输入考试要求" v-model="formData.askExam" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="心得要求">
            <el-input  placeholder="请输入心得要求" v-model="formData.askEtc" />
          </el-form-item>
        </el-col>
      </el-row>
       <el-row>
        <el-col :span="12">
          <el-form-item label="开班日期">
             <el-date-picker
                v-model="formData.startDate"
                value-format="YYYY-MM-DD"
                type="date"
                placeholder="请选择结束日期"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="毕业日期">
             <el-date-picker
                v-model="formData.endDate"
                value-format="YYYY-MM-DD"
                type="date"
                placeholder="请选择结束日期"
            />
          </el-form-item>
        </el-col>
      </el-row>
     
      <el-form-item label="班级简介">
        <el-input  type="textarea" :rows="3" v-model="formData.info"/>
      </el-form-item>

      <el-form-item label="">
        <el-button type="primary" @click="onSubmit">确定</el-button>
        <el-button type="default" @click="state = false">取消</el-button>
      </el-form-item>
    </el-form>
    <!-- 内容 -->
  </el-dialog>
</template>

<style>
.dvimg{
  width: 100px;
  height: 100px;
}
</style>
